#include "ActionPanel.h"
#include "../../core/Impp.h"

namespace Impp
{

ActionLabel::ActionLabel(const wxString & actionClassName, Action* action) :
	actionClassName(actionClassName),label(NULL)
{
	addAction(action);
}

ActionLabel::~ActionLabel()
{

}

wxStaticText* ActionLabel::getLabel()
{
	return label;
}

wxString ActionLabel::getActionClassName()
{
	return actionClassName;
}

void ActionLabel::setLabel(wxStaticText* label)
{
	this->label=label;
}

void ActionLabel::addAction(Action* action)
{
	action->lock();
	actions.push_back(action);
	if(label != NULL){
		label->SetLabel(getLabelString());
	}
}

bool ActionLabel::removeAction(Action* action){
	std::vector<Action*>::iterator iter(actions.begin());
	for (; iter != actions.end(); ++iter) {
		if(*iter == action){
			actions.erase(iter);
			action->unLock();
			if(label != NULL){
				label->SetLabel(getLabelString());
			}
			return true;
		}
	}
	return false;
}

size_t ActionLabel::getActionsCount(){
	return actions.size();
}

wxString ActionLabel::getLabelString()
{
	if(actions.empty()){
		return actionClassName;
	}
	if(actions.size() == 1){
		return actions[0]->getFullDescription();
	}

	return actions[0]->getDescription() <<  wxT(" (") << actions.size() << wxT(")");
}


ActionPanel::ActionPanel(BuddyList* list, wxWindow* parent,const int & id) :
	wxPanel(parent, id),
	list(list)
{
	sizer = new wxBoxSizer(wxVERTICAL);

	ActionManager* actionManager = Impp::exemplar()->getActionManager();
	if(actionManager != NULL){
		actionManager->Connect(EVT_ACTION, ActionEventHandler(ActionPanel::onActionEvent),NULL, this);
	}else{
		wxLogError(wxT("ActionLabel: ActionManager was NULL!"));
	}

	SetSizer(sizer);
}

ActionPanel::~ActionPanel()
{
	std::vector<ActionLabel*>::iterator iter(actionLabels.begin());
	for (; iter != actionLabels.end(); ++iter){
			ActionLabel* acLabel = *iter;
			acLabel->getLabel()->Destroy();
			delete acLabel;
	}
}

void ActionPanel::onActionEvent(ActionEvent & event){

	switch (event.getActionEventType()) {
		case ACTION_EVENT_NEW_ACTION:
			{
				ActionLabel* acLabel = NULL;
				Action* action = event.getAction();
				//ActionId id = event.getActionId();
				wxString className = event.getActionClassName();

				std::vector<ActionLabel*>::iterator iter(actionLabels.begin());
				for (; iter != actionLabels.end(); ++iter) {
					if((*iter)->getActionClassName() == className){
						acLabel = *iter;
						break;
					}
				}

				if(acLabel == NULL){
					acLabel = new ActionLabel(className, action);
					wxStaticText* label = new wxStaticText(this,wxID_ANY,acLabel->getLabelString());
					acLabel->setLabel(label);
					sizer->Add(label, 1,wxEXPAND|wxALL,0);
					actionLabels.push_back(acLabel);
					sizer->Layout();
					list->layoutMainSizer();
				}else{
					acLabel->addAction(action);
				}

			}
			break;
		case ACTION_EVENT_DELETE_ACTION:
			{
				ActionLabel* acLabel = NULL;
				//ActionId id = event.getActionId();
				Action* action = event.getAction();

				std::vector<ActionLabel*>::iterator iter(actionLabels.begin());
				for (; iter != actionLabels.end(); ++iter) {
					if((*iter)->removeAction(action)){
						acLabel = *iter;
						if(acLabel->getActionsCount() == 0){
							sizer->Detach(acLabel->getLabel());
							sizer->Layout();
							list->layoutMainSizer();

							acLabel->getLabel()->Destroy();

							actionLabels.erase(iter);
							delete acLabel;
							acLabel = NULL;
						}
						break;
					}
				}


			}
			break;

		default:
			break;
	}

}

}
